﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Oracle.ManagedDataAccess.Client;

namespace YJ.Data.ORACLE
{
    public class DocumentDirectory : YJ.Data.Interface.IDocumentDirectory
    {
        private DBHelper dbHelper = new DBHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public DocumentDirectory()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.DocumentDirectory实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(YJ.Data.Model.DocumentDirectory model)
        {
            string sql = @"INSERT INTO DocumentDirectory
				(ID,ParentID,Name,ReadUsers,ManageUsers,PublishUsers,Sort) 
				VALUES(:ID,:ParentID,:Name,:ReadUsers,:ManageUsers,:PublishUsers,:Sort)";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = model.ID },
				new OracleParameter(":ParentID", OracleDbType.Varchar2){ Value = model.ParentID },
				new OracleParameter(":Name", OracleDbType.Varchar2){ Value = model.Name },
				model.ReadUsers == null ? new OracleParameter(":ReadUsers", OracleDbType.Varchar2) { Value = DBNull.Value } : new OracleParameter(":ReadUsers", OracleDbType.Varchar2) { Value = model.ReadUsers },
				model.ManageUsers == null ? new OracleParameter(":ManageUsers", OracleDbType.Varchar2) { Value = DBNull.Value } : new OracleParameter(":ManageUsers", OracleDbType.Varchar2) { Value = model.ManageUsers },
				model.PublishUsers == null ? new OracleParameter(":PublishUsers", OracleDbType.Varchar2) { Value = DBNull.Value } : new OracleParameter(":PublishUsers", OracleDbType.Varchar2) { Value = model.PublishUsers },
				new OracleParameter(":Sort", OracleDbType.Int32){ Value = model.Sort }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.DocumentDirectory实体类</param>
        public int Update(YJ.Data.Model.DocumentDirectory model)
        {
            string sql = @"UPDATE DocumentDirectory SET 
				ParentID=:ParentID,Name=:Name,ReadUsers=:ReadUsers,ManageUsers=:ManageUsers,PublishUsers=:PublishUsers,Sort=:Sort
				WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ParentID", OracleDbType.Varchar2){ Value = model.ParentID },
				new OracleParameter(":Name", OracleDbType.Varchar2){ Value = model.Name },
				model.ReadUsers == null ? new OracleParameter(":ReadUsers", OracleDbType.Varchar2) { Value = DBNull.Value } : new OracleParameter(":ReadUsers", OracleDbType.Varchar2) { Value = model.ReadUsers },
				model.ManageUsers == null ? new OracleParameter(":ManageUsers", OracleDbType.Varchar2) { Value = DBNull.Value } : new OracleParameter(":ManageUsers", OracleDbType.Varchar2) { Value = model.ManageUsers },
				model.PublishUsers == null ? new OracleParameter(":PublishUsers", OracleDbType.Varchar2) { Value = DBNull.Value } : new OracleParameter(":PublishUsers", OracleDbType.Varchar2) { Value = model.PublishUsers },
				new OracleParameter(":Sort", OracleDbType.Int32){ Value = model.Sort },
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = model.ID }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(Guid id)
        {
            string sql = "DELETE FROM DocumentDirectory WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = id }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<YJ.Data.Model.DocumentDirectory> DataReaderToList(OracleDataReader dataReader)
        {
            List<YJ.Data.Model.DocumentDirectory> List = new List<YJ.Data.Model.DocumentDirectory>();
            YJ.Data.Model.DocumentDirectory model = null;
            while (dataReader.Read())
            {
                model = new YJ.Data.Model.DocumentDirectory();
                model.ID = dataReader.GetString(0).ToGuid();
                model.ParentID = dataReader.GetString(1).ToGuid();
                model.Name = dataReader.GetString(2);
                if (!dataReader.IsDBNull(3))
                    model.ReadUsers = dataReader.GetString(3);
                if (!dataReader.IsDBNull(4))
                    model.ManageUsers = dataReader.GetString(4);
                if (!dataReader.IsDBNull(5))
                    model.PublishUsers = dataReader.GetString(5);
                model.Sort = dataReader.GetInt32(6);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.DocumentDirectory> GetAll()
        {
            string sql = "SELECT * FROM DocumentDirectory";
            OracleDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.DocumentDirectory> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM DocumentDirectory";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public YJ.Data.Model.DocumentDirectory Get(Guid id)
        {
            string sql = "SELECT * FROM DocumentDirectory WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = id.ToString() }
			};
            OracleDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.DocumentDirectory> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }

        /// <summary>
        /// 得到所有下级栏目(公共栏目)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<Data.Model.DocumentDirectory> GetChilds(Guid id)
        {
            string sql = "SELECT * FROM DocumentDirectory WHERE ParentID=:ParentID ORDER BY Sort";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ParentID", OracleDbType.Varchar2){ Value = id.ToString() }
			};
            OracleDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.DocumentDirectory> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }

        /// <summary>
        /// 得到最大排序
        /// </summary>
        /// <param name="dirID"></param>
        /// <returns></returns>
        public int GetMaxSort(Guid dirID)
        {
            string sql = "SELECT MAX(Sort) Sort FROM DocumentDirectory WHERE ParentID=:ParentID ";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ParentID", OracleDbType.Varchar2){ Value = dirID.ToString() }
			};
            var number = dbHelper.GetFieldValue(sql, parameters).ToInt(0);
            return number + 5;
        }
    }
}